from .baseGraphNode import  *
from ..execute.baseExeHis import BaseExeHis


class MessageConcatNode(BaseGraphNode):
    def __init__(self, id, data, graphNodeManager, random_key):
        super().__init__(id, data, graphNodeManager, random_key)
        if 'custom_keys' in data['data']:
            self.custom_keys = data['data']['custom_keys'] 
            '''
                {
                id: "custom-key-name-msg"
                name : "name"
                note: "the name of the tool"
                type:'str'}
            '''
        
        self.executed_num = 1
        for custom_key in self.custom_keys:
            self.dict['out'][custom_key['name']] = {'type':custom_key['type'], 'note':custom_key['note']}
            self.dict['out'][custom_key['id']] = {}

    def get_newest_output_in_session(self, session_info, target_handle):
        '''
        return the concat message of the custom_keys
        '''
        message = []
        for custom_key in self.custom_keys:
            if custom_key['name'] in self.dict['in']:
                custom_key_from = self.dict['in'][custom_key['name']]
                prenode = self.graphNodeManager.nodesByIdDict[custom_key_from['nodeId']]
                custom_key_value = prenode.get_newest_output_in_session(session_info, custom_key_from['handle'])
                if isinstance(custom_key_value, list):
                    message.extend(custom_key_value)
                else:
                    message.append(custom_key_value)
        return message
